package com.linran.utils.myenum;

import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lin
 * @date 2020-08-17 10:17
 */
public enum PayrollDay {
    /**
     *
     */
    MONDAY, TUESDAY, WENDESDAY, THURSDAY, FRIDAY, STATURDAY(PayType.WEEKEND), SUNDAY(PayType.WEEKEND);

    public final PayType payType;

    PayrollDay() {
        this.payType = PayType.WEENKDAY;
    }

    PayrollDay(PayType payType) {
        this.payType = payType;
    }

    int pay(int minutesWorked, int payRate) {
        //通过具体的PayType枚举值去调用方法pay
        return payType.pay(minutesWorked, payRate);
    }

    public static final Map<String, PayrollDay> STRING_TO_ENUM = Stream.of(values()).collect(Collectors.toMap(PayrollDay::toString, UnaryOperator.identity()));

    public static PayrollDay fromString(String str) throws Exception {
        //获取不到值则抛异常
        return Optional.of(STRING_TO_ENUM.getOrDefault(str, null)).orElseThrow(() -> new Exception("PayrollDay转换一场"));
    }

    private enum PayType {
        /**
         *
         */
        WEENKDAY {
            @Override
            int overtimePay(int mins, int payRate) {
                return mins <= MINS_PER_SHIFT ? 0 : (mins - MINS_PER_SHIFT) * payRate / 2;
            }
        },
        WEEKEND {
            @Override
            int overtimePay(int mins, int payRate) {
                return mins * payRate / 2;
            }
        };

        abstract int overtimePay(int mins, int payRate);

        public static final int MINS_PER_SHIFT = 8 * 60;

        int pay(int minsWorked, int payRate) {
            int basePay = minsWorked <= MINS_PER_SHIFT ? minsWorked * payRate : MINS_PER_SHIFT * payRate;
            return basePay + overtimePay(minsWorked, payRate);
        }
    }
}

